package com.yc.home.cloudother.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yc.bean.Result;
import com.yc.entity.Makeno;
import com.yc.entity.Question;
import com.yc.home.cloudother.mapper.MakenoMapper;
import com.yc.home.cloudother.mapper.QuestionMapper;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Timestamp;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lzk
 * @since 2024-09-26
 */
@RestController
@RequestMapping("/makeno")
public class MakenoController {

    @Autowired
    MakenoMapper makenoMapper;

    @Autowired
    QuestionMapper questionMapper;

    @RequestMapping("getno")
    Page getno(@RequestParam(defaultValue = "1") int page,
                 @RequestParam(defaultValue = "10") int size, String input, String uid){
        Page<Makeno> pageObj = new Page<>(page, size);

        // 条件设置
        LambdaQueryWrapper<Makeno> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Makeno::getUid, uid);

        // 执行分页查询
        Page<Makeno> makenoPage = makenoMapper.selectPage(pageObj, lqw);

        // 使用Stream API转换Doquestion为Question并更新Page对象的records
        List<dono> questionList = makenoPage.getRecords().stream()
                .map(doquestion -> {
                    LambdaQueryWrapper<Question> lqw1 = new LambdaQueryWrapper<>();
                    lqw1.eq(Question::getId, doquestion.getQid());
                    lqw1.like(Question::getContent, "%"+ input +"%");
                    Question question = questionMapper.selectOne(lqw1);
                    if(question == null){
                        return null;
                    }
                    dono no = new dono();
                    no.setAnswer(doquestion.getAnswer());
                    no.setId(question.getId());
                    no.setContent(question.getContent());
                    no.setCourse(question.getCourse());
                    no.setIdeas(question.getIdeas());
                    no.setItems(question.getItems());
                    no.setImage(question.getImage());
                    no.setCreateTime(question.getCreateTime());
                    no.setType(question.getType());
                    no.setMid(String.valueOf(doquestion.getId()));
                    return no;
                }).filter(Objects::nonNull)
                .collect(Collectors.toList());

        //替换数据
        Page<dono> questionPage = new Page<>(makenoPage.getCurrent(), makenoPage.getSize(), makenoPage.getTotal());
        questionPage.setRecords(questionList);
        return questionPage;
    }

    //删除错题集
    @RequestMapping("del")
    Result del(@RequestParam String id){
        try{
            makenoMapper.deleteById(id);
            return new Result(1, "删除成功", null);
        }catch (Exception e){
            return new Result(0, "删除失败", null);
        }
    }

}

@Data
class dono {
    private Integer id;

    private String content;

    private String course;

    private String type;

    private String items;

    private Timestamp createTime;

    private String ideas;

    private String image;

    private String answer;

    private String mid;
}

